﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using WUTOS.DataBase.DataBaseDAL;

namespace WUTOS.DataBase
{
    public partial class AccountAdminFrame : Form
    {
        #region -数据库版本及身份验证模式-
        List<BLL.databaseversion> databaseVersion = BLL.GetLocalDataBaseVersion();
        DataBaseInfo databaseMode = new DataBaseInfo();
        bool pwdChecked;
        #endregion

        public AccountAdminFrame()
        {
            InitializeComponent();
            databaseMode.InitialCatalog = "master";
            databaseMode.IntegratedSecurity = true;
            databaseMode.ConnectTimeout = 30;
        }

        private void AccountAdminFrame_Load(object sender, EventArgs e)
        {
            BindDbEngineVersion();
        }
        /// <summary>
        /// 绑定数据
        /// </summary>
        private void BindDbEngineVersion()
        {
            List<string> dbVersion = new List<string>();
            dbVersion.Add("");
            foreach (BLL.databaseversion Version in databaseVersion)
            {
                dbVersion.Add(Version.version);
            }
            cbDbEngine.DataSource = dbVersion;

            BLL.databaseversion Ver = BLL.ReadConfigXml();
            for (int i = 0; i < this.cbDbEngine.Items.Count; i++)
            {
                if (Ver.version == cbDbEngine.Items[i].ToString())
                {
                    this.cbDbEngine.SelectedIndex = i;
                    break;
                }
            }
            cbDbEngine_SelectedIndexChanged(null, null);
        }

        private bool IsAccountExit()
        {
            string msg="";
            List<string> accountList = BLL.GetSqlServerUserName(1, databaseMode, ref msg);
            foreach (string name in accountList)
            {
                if (txtAccount.Text==name)
                {
                    this.txtAccount.Clear();
                    this.txtPwd.Clear();
                    this.txtPwdConfirm.Clear();
                    this.chbPwdChecked.Checked = false;
                    this.ActiveControl = this.txtAccount;
                    this.errorProvider.SetError(txtAccount, "用户名已经存在,请重新输入");
                    return true;
                }
            }
            return false;
                
        }

        private void btnAddAccount_Click(object sender, EventArgs e)
        {
            string msg="";
            this.errorProvider.Clear();
            if (!IsBlank()!=IsAccountExit())
            {
                //服务器类型：true：MSSQLSERVER，false：SQLEXPRESS
                if (!cbDbEngine.Text.ToUpper ().Contains("EXPRESS"))
                {
                    databaseMode.DataBaseSourse = "localhost";
                }
                else
                {
                    databaseMode.DataBaseSourse = @"localhost\SQLEXPRESS";
                } 
                pwdChecked = chbPwdChecked.Checked;
                if(BLL.AddDataBaseUser(1,databaseMode,txtAccount.Text,txtPwd.Text,pwdChecked,ref msg))
                {
                    MessageBox.Show("成功添加用户","系统提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
                    this.Close();
                }
                else
                {
                    this.errorProvider.SetError(chbPwdChecked, msg);
                }
            }
        }

        private bool IsBlank()
        {
            if (cbDbEngine.Text=="")
            {
                this.errorProvider.SetError(this.cbDbEngine, "数据库引擎为空!");
                this.ActiveControl =this.cbDbEngine;
                return true;  
            }

            if (txtAccount.Text=="")
            {
                this.errorProvider.SetError(this.txtAccount, "用户名为空!");
                this.ActiveControl = this.txtAccount;
                return true;
            }
            if (txtPwd.Text=="")
            {
                this.errorProvider.SetError(this.txtPwd, "密码为空!");
                this.ActiveControl = this.txtPwd;
                return true;
            }
            if (txtPwdConfirm.Text=="")
            {
                this.errorProvider.SetError(this.txtPwdConfirm, "确认密码为空!");
                this.ActiveControl = this.txtPwdConfirm;
                return true;
            }
            if (this.txtPwd.Text!=this.txtPwdConfirm.Text)
            {
                this.errorProvider.SetError(this.txtPwd, "初始密码与确认密码不符!");
                this.txtPwd.Clear();
                this.txtPwdConfirm.Clear();
                this.ActiveControl = this.txtPwd;
                return true;
            }
            return false;
        }

        private void cbDbEngine_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (BLL.databaseversion Version in databaseVersion)
            {
                if (Version.version == cbDbEngine.Text)
                {
                    int LoginMode = Version.loginmode;
                    //Windows模式
                    if (LoginMode == 1)
                    {
                        this.chbPwdChecked.Visible = true;
                        chbPwdChecked.Enabled = false;
                    }
                    //LoginMode=2，混合模式
                    else
                    {
                        this.chbPwdChecked.Visible = true;
                        chbPwdChecked.Enabled = true;
                    }
                    chbPwdChecked.Checked = false;
                    break;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
 
    }
}
